﻿@page "/files/{cipher}"
@using Microsoft.Extensions.Options
@using Tmp.Models

<RadzenCard>
    <RadzenStack>
        <RadzenTextBox @bind-Value="@_cipher" class="w-100"></RadzenTextBox>
        <RadzenButton Click="GoesTo" Text="前往" ButtonStyle="ButtonStyle.Primary"></RadzenButton>
    </RadzenStack>
</RadzenCard>
<style>
    .custom-link {
        display: inline-block;
        padding: 8px 16px;
        border: 1px solid #ccc;
        border-radius: 4px;
        text-align: center;
        text-decoration: none;
        color: #000;
        background-color: #eee;
    }
</style>
<RadzenCard @key="Cipher">
    @if (_files == null || !_files.Any())
    {
        <RadzenText TextStyle="TextStyle.DisplayH5" Text="找不到文件"/>
    }
    else
    {
        <h5>文件列表</h5>
        @foreach (var filePath in _files)
        {
            <RadzenCard>
                <div class="d-flex justify-content-between">
                    <span>@filePath</span>

                    <RadzenLink Path="@($"/download/file/{Cipher}/{filePath}")" Text="下载"
                                class="custom-link"
                                download="@($"{filePath}")"
                    ></RadzenLink>
                    @* download="@($"{filePath}")" *@
                </div>
            </RadzenCard>
        }
    }
</RadzenCard>
@code {

    [Parameter] public string? Cipher { get; set; }
    [Inject]
    IOptions<Configuration>? Options { get; set; }

    [Inject]
    private NavigationManager? NavigationManager { get; set; }

    private string? _cipher;

    private void GoesTo()
    {
        bool force = Cipher != _cipher;
        NavigationManager?.NavigateTo($"/files/{_cipher}", forceLoad: force);
    }

    List<string>? _files;
    protected async override Task OnInitializedAsync()
    {
        _cipher = Cipher;
        string folderPath = Path.Combine(Options.Value.UploadPath, "" + Cipher);
        if (!Directory.Exists(folderPath))
        {
            return;
        }
        string[] filePaths = Directory.GetFiles(folderPath);
        _files = filePaths.Select(Path.GetFileName).ToList();
    }
}